var folderplus = {

	/**
	 * Go to folder of current message Always use the first tab of type "Folder"
	 * Don't use a tab as gloda search Change folder even if message already
	 * visible in a virtual folder.
	 */
	goToFolder : function() {
		var aMsgHdr = gFolderDisplay.selectedMessage;
		var folder = gFolderTreeView.getFolderForMsgHdr(aMsgHdr);
		this._GoToFolder(folder, aMsgHdr);
	},

	/**
	 * Same as goToFolder. Maybe it will be changed in future :-/
	 */
	goToThread : function() {
		var aMsgHdr = gFolderDisplay.selectedMessage;
		var folder = GetMsgFolderFromUri(folderplus.folderURI);
		this._GoToFolder(folder, aMsgHdr);
	},

	/**
	 * Go to folder of current message Always use the first tab of type "Folder"
	 * Don't use a tab as gloda search Change folder even if message already
	 * visible in a virtual folder.
	 */
	_GoToFolder : function(folder, aMessage) {
		// inspired from MsgDisplayMessageInFolderTab
		// but force using first tab to manage virdual folder.
		var tabmail = document.getElementById("tabmail");
		var folderTabs = tabmail.tabModes["folder"];
		var folderTab = folderTabs.tabs[0];
		var folderDisplay = folderTab.folderDisplay;
		folderDisplay.selectMessageComingUp();
		tabmail.switchToTab(folderTab);
		if (folderDisplay.displayedFolder != folder
				|| folderDisplay.view.isVirtual) {
			// Force select the folder
			folderDisplay.show(folder);
			gFolderTreeView.selectFolder(folder, true);
		}
		// Force select the message
		if (aMessage) folderDisplay.selectMessage(aMessage, true);
	},

	moveAndGoToThreadFolder : function() {
		var aMsgHdr = gFolderDisplay.selectedMessage;
		var destMsgFolder = GetMsgFolderFromUri(folderplus.folderURI);
		// use MsgMoveMessage to set "move again"
		MsgMoveMessage(destMsgFolder);
		this._GoToFolder(destMsgFolder, aMsgHdr);
	},
	
	moveToThreadFolder : function() {
		var destMsgFolder = GetMsgFolderFromUri(folderplus.folderURI);
		// use MsgMoveMessage to set "move again"
		MsgMoveMessage(destMsgFolder);
	},
	
	/*
	 * folderURI to move move message, setted by last fillMenuxxx Not very clean
	 * but avoid to laucnh a gloda search twice. :-/
	 */
	folderURI : null,

	/**
	 * set move to conversation and go to folder menu in mailContext menu
	 * and go menu.
	 * Async method :-/ First call a globa search to get a gloda message from 
	 * aMsgHdr then call a second gloda search to find all messages from the 
	 * conversation.
	 */
	fillMailContextMenu : function(event) {
		// cf msgHdrViewOverlay.js/ConversationOpener
		var aMsgHdr = gFolderDisplay.selectedMessage;
		var bundle = document.getElementById("folderplus-strings");
		var value = aMsgHdr.folder.prettyName;
		var aLabel = bundle.getFormattedString("folderplus.GoToFolder",[ value ], 1);
		var aMenuItem = document.getElementById("folderplus.GoToFolder.mailContext");
		aMenuItem.label = aLabel;
		aMenuItem = document.getElementById("folderplus.GoToFolder.goMenu");
		aMenuItem.label = aLabel;

		var aConvListener = {
			/*
			 * called when new items are returned by the database query or
			 * freshly indexed
			 */
			onItemsAdded : function myListener_onItemsAdded(aItems, aCollection) {
			},
			/*
			 * called when items that are already in our collection get
			 * re-indexed
			 */
			onItemsModified : function myListener_onItemsModified(aItems,
					aCollection) {
			},
			/*
			 * called when items that are in our collection are purged from the
			 * system
			 */
			onItemsRemoved : function myListener_onItemsRemoved(aItems,
					aCollection) {
			},
			/* called when our database query completes */
			onQueryCompleted : function myListener_onQueryCompleted(aCollection) {
				try {
					if (!aCollection.items.length) {
						Components.utils
								.reportError("Couldn't find a collection for msg: ");
					} else {
						var folderURI = folderplus._findConversationFolder(aCollection.items);
						// keep folder for futur command
						folderplus.folderURI = folderURI;
						// get the msg folder we're moving messages into
						var destMsgFolder = GetMsgFolderFromUri(folderURI);
						var value = destMsgFolder.prettyName;
						folderplus._updateAllMenus(value);
					}
				} catch (e) {
					Components.utils.reportError(e);
				}
			}
		};
		var aListener = {
			/*
			 * called when new items are returned by the database query or
			 * freshly indexed
			 */
			onItemsAdded : function myListener_onItemsAdded(aItems, aCollection) {
			},
			/*
			 * called when items that are already in our collection get
			 * re-indexed
			 */
			onItemsModified : function myListener_onItemsModified(aItems,
					aCollection) {
			},
			/*
			 * called when items that are in our collection are purged from the
			 * system
			 */
			onItemsRemoved : function myListener_onItemsRemoved(aItems,
					aCollection) {
			},
			/* called when our database query completes */
			onQueryCompleted : function myListener_onQueryCompleted(aCollection) {
				try {
					if (!aCollection.items.length) {
						folderplus.folderURI = null;
						Components.utils.reportError("Couldn't find a collection for msg: ");
					} else {
						var aGlodaMessage = aCollection.items[0];
						var aConvCollection = aGlodaMessage.conversation
								.getMessagesCollection(aConvListener);
					}
				} catch (e) {
					folderplus.folderURI = null;
					Components.utils.reportError(e);
				}
			}
		};
		var aCollection = Gloda.getMessageCollectionForHeader(aMsgHdr, aListener);
	},
	
	/**
	 * The function which decide which Folder use as the conversation folder.
	 * Currenlty, use the folder of the first message of the conversation.
	 * @params message collection
	 @return folderURI
	 */
	_findConversationFolder : function (items) {
		var firstDate = null;
		var firstMessage = null;
		for ( var i = 0; i < items.length; i++) {
			var aItem = items[i];
			var aDate = aItem.folderMessage.date;
			var aFolder = aItem.folderMessage.folder.URI;
			if (firstDate == null || (aDate < firstDate)) {
				firstDate    = aItem.folderMessage.date;
				firstMessage = aItem; 
			}
		}
		return firstMessage.folderURI;
	},
	
	/**
	 * Update all needed menu labels with current folder and conversation name.
	 */
	_updateAllMenus : function (value) {
		var bundle = document.getElementById("folderplus-strings");
		var aLabel     = bundle.getFormattedString("folderplusMoveToConversation", [ value ], 1);
		var aLabelMenu = bundle.getFormattedString("folderplusMoveToConversationMenu", [ value ], 1);
		var aMenuItem = document.getElementById("folderplus.moveToConversationFolder");
		aMenuItem.label = aLabel;
		aMenuItem = document.getElementById("folderplus.GoToConversation");
		aMenuItem.label = aLabel;
		aMenuItem = document.getElementById("folderplus.GoToConversation.goMenu");
		aMenuItem.label = aLabel;
		aMenuItem = document.getElementById("folderplus.moveToConversationFolder.messageMenu");
		aMenuItem.label = aLabelMenu;
			}
};
